<template>
  <div class="main1">
    <div class="login_form">
      <h2>网易考拉后台管理系统</h2>
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input placeholder="请输入密码" v-model="form.password" show-password></el-input>
        </el-form-item>

        <el-form-item prop="keep">
          <el-checkbox v-model="form.checked" @click.native="keep($event)">保留7天免登陆</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('form')" style="float:right;">登陆</el-button>
          <el-button @click="gotoReg" style="float:right;margin-right:5px">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import usersApi from "@/api/usersApi";
import {setToken,setUser} from "@/utils/auth";

export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
        checked:false
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          //验证通过，可以发送请求
          try {
            let password = this.form.password;
            let p = await usersApi.login(this.form.username,password);
            if (p.data.flag) {
              //登陆成功:把token和用户信息存到本地(cookie)，跳转到首页
              this.$message({
                message: "登陆成功",
                type: "success"
              });
              //存储token和用户信息到本地
              let userinf = {
                username: this.form.username,
                uid: p.data.uid
              };
              console.log(userinf);
              if (this.form.checked) {
                //需要保持7天
                setToken(p.data.token, 7);
                setUser(JSON.stringify(userinf), 7);
              } else {
                //存成会话级别:关掉浏览器就删除
                setToken(p.data.token);
                setUser(JSON.stringify(userinf));
              }

              //跳转到首页
              this.$router.push("/home");
            } else {
              //登陆失败
              this.$message({
                message: "登陆失败",
                type: "error"
              });
            }
          } catch (err) {}
        } else {
          // console.log("error submit!!");
          this.$message({
            message: "服务器问题",
            type: "error"
          });
          return false;
        }
      });
    },
    gotoReg() {
      this.$router.push("/register");
    },
    keep(ev) {
      if (ev.target.tagName == "INPUT") return;
      if (!this.form.checked) {
        this.$message({
          message: "请不要在公共场合使用该功能",
          type: "error"
        });
      }
    }
  }
};
</script>
    
<style>
.main1 {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url("../../assets/img/login.jpg");
  overflow: hidden;
}

.login_form {
  width: 350px;
  /* 上下间隙150px, 左右自动 */
  margin: 160px auto;
  /*透明背景色*/
  background-color: rgb(255, 255, 255, 0.8);
  padding: 30px;
  padding-right: 60px;
  border-radius: 20px;
}
</style>